<?php
/*
# TCE - Tight Control Environment: Hosting Platform
# Copyright (c) 2006 by the TightControl Dev Team
# http://tce.mystichost.net
#
#
# License:
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of the MPL Mozilla Public License
#    as published by the Free Software Foundation; either version 1.1
#    of the License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    MPL Mozilla Public License for more details.
#
#    You may have received a copy of the MPL Mozilla Public License
#    along with this program.
#
#    An on-line copy of the MPL Mozilla Public License can be found
#    http://www.mozilla.org/MPL/MPL-1.1.html
#
#
# The TCE Home Page is at:
#
#    http://tce.mystichost.net
*/
/* Define TCE_LIB_INCLUDE_PATH */
define('TCE_LIB_INCLUDE_PATH', realpath(dirname(__FILE__)) . '/');

/* Check if the TCE-config.conf file exists */
if (!file_exists(TCE_LIB_INCLUDE_PATH . 'tce.conf')){
	/*
	No, it does not, so we can only load the files that do not depend on the DB backend
	Example usage: First time configuration
	*/
	define('TCE_CONF_FOUND', false);
	define('TCE_CONF_PATH', '');
}else{
	/*
	Yes, it exists, so include everything
	*/
	define('TCE_CONF_FOUND', true);
	define('TCE_CONF_PATH', TCE_LIB_INCLUDE_PATH . 'tce.conf');
}

/* Include all required files */
include_once (TCE_LIB_INCLUDE_PATH . 'Exceptions.classes.php');
include_once (TCE_LIB_INCLUDE_PATH . 'Config.class.php');
include_once (TCE_LIB_INCLUDE_PATH . 'Syslog.class.php');
/* Load the controllers */
/* Master abstraction controller */
include_once (TCE_LIB_INCLUDE_PATH . 'Controllers/Controller.php');
/* The FTP abstraction controller */
include_once (TCE_LIB_INCLUDE_PATH . 'Controllers/FTP_Controller.php');
/* The Cmd Execution abstraction controller */
include_once (TCE_LIB_INCLUDE_PATH . 'Controllers/CMD_Controller.php');

/* Check if the tce.conf file was found */
if (TCE_CONF_FOUND){
	/* Yes, so load the required sections */
	/* But first parse the configuration */
	/* Try to parse the configuration */
	try {
		$Config = Config::Factory(TCE_CONF_PATH);
	}catch( ConfigException $e )
	{
		die('The master configuration file contains an error. Please, contact your webhost.');
	}catch( FileException $e )
	{
		die('The master configuration file could not be parsed. Please, contact your webhost.');
	}catch( Exception $e )
	{
		die('An unknown error occured. Please, contact your webhost.');
	}

	/* Okay, we got the configuration, check and fix if needed or die() on invalid! */
	$ConfigurationValid = true; // Asume it is at start

	/* Check if we have a DSN set */
	if (strval($Config->DATABASE_DSN) == ''){
		$ConfigurationValid = false;
	}
	/* Check if we have a INSTALL_TYPE set */
	if (strval($Config->INSTALL_TYPE) == ''){
		$Config->INSTALL_TYPE = 'master';
	}
	/* Check if we have a ROOT_DIR set */
	if (strval($Config->ROOT_DIR) == ''){
		$ConfigurationValid = false;
	}
	/* Check if we have a CONF_DIR set */
	if (strval($Config->CONF_DIR) == ''){
		$ConfigurationValid = false;
	}
	/* Check if we have a LOG_DIR set */
	if (strval($Config->LOG_DIR) == ''){
		$ConfigurationValid = false;
	}
	/* Check if we have a LIB_DIR set */
	if (strval($Config->LIB_DIR) == ''){
		$ConfigurationValid = false;
	}
	/* Check if we have a LIB_DIR set */
	if (strval($Config->LIB_DIR) == ''){
		$Config->LIB_DIR = '/tmp';
	}
	/* Check if we have a LANGUAGE_MULTI set */
	if ($Config->LANGUAGE_MULTI == null){
		$Config->LANGUAGE_MULTI = false;
	}
	/* Check if we have a LANGUAGE_DEFAULT set */
	if (strval($Config->LANGUAGE_DEFAULT) == ''){
		$Config->LANGUAGE_DEFAULT = 'english';
	}
	/* Check if the config was valid */
	if (!$ConfigurationValid){
		die('The master configuration file is not correctly configured. Please, contact your webhost.');
	}

	// TODO (sinnerg, 13/10/2006)
	include_once (TCE_LIB_INCLUDE_PATH . 'Syslog.class.php');
	include_once ('MDB2'); // MDB2 is used for database access

	/* Connect to the database */
	$DB =& MDB2::singleton($Config->DATABASE_DSN);
	if (PEAR::isError($DB)) {
		unset($DB);
		die('Could not connect to the Tight Control master database. Please, contact your webhost if the problem persists.');
	}else{
		define('TCE_MASTER_DSN', $Config->DATABASE_DSN);
		// Set some options
		$DB->setFetchMode(MDB2_FETCHMODE_ASSOC);
		$DB->setOption('emulate_prepared', true);
	}
	/* Everything is configured and O K */
	define('TCE_CONFIGURED', true);
}else{
	/* Config file not found, so set it to false */
	/* This way we can detect problems and react on them */
	/* A normal page should return a message stating that TCE has not been configured yet! */
	define('TCE_CONFIGURED', false);
}

/* Last step : Load the Controllers */
/* FTP Controllers */
FTP_Controller::loadControllers();
/* CMD Controllers */
CMD_Controller::loadControllers();

?>